<?php
namespace Think\Upload\Driver\Upload;
/**
 * Created by PhpStorm.
 * User: thankful
 * Date: 2017/8/21
 * Time: 14:29
 */
class TKFile
{
    /**
     * 上传文件
     * @param array $data :{
     *  url:'http://tong-kuai.com',
     *  file:object[file],
     *  data:array
     * }
     * @return string
     */
    public static function uploadFileTK($data = array())
    {
        $url = $data['url'];
        if (empty($url)) {
            return 'url undefined';
        }
        $file = $data['file'];
        if (empty($file)) {
            return 'file undefined';
        }
        return self::curl_post($url, $data['post'], array('file' => $file));
    }

    /**
     * @param $url 请求地址
     * @param $data post表单数据
     * @param $file post文件
     * @return mixed
     */
    private static function curl_post($url, $data = array(), $file = array())
    {
        $header = array(
            'Content-Type: multipart/form-data',
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        self::curl_custom_postfields($ch, $data, $file);
        $dxycontent = curl_exec($ch);
        curl_close($ch);
        return $dxycontent;

    }

    /**
     * For safe multipart POST request for PHP5.3 ~ PHP 5.4.
     *
     * @param resource $ch cURL resource
     * @param array $assoc "name => value"
     * @param array $files "name => path"
     * @return bool
     */
    private static function curl_custom_postfields($ch, array $assoc = array(), array $files = array())
    {
        // invalid characters for "name" and "filename"
        static $disallow = array("\0", "\"", "\r", "\n");

        // build normal parameters
        foreach ($assoc as $k => $v) {
            $k = str_replace($disallow, "_", $k);
            $body[] = implode("\r\n", array(
                "Content-Disposition: form-data; name=\"{$k}\"",
                "",
                filter_var($v),
            ));
        }

        // build file parameters
        foreach ($files as $k => $v) {
            switch (true) {
                case false === $v = realpath(filter_var($v)):
                case !is_file($v):
                case !is_readable($v):
                    continue; // or return false, throw new InvalidArgumentException
            }
            $data = file_get_contents($v);
            $v = call_user_func("end", explode(DIRECTORY_SEPARATOR, $v));
            $k = str_replace($disallow, "_", $k);
            $v = str_replace($disallow, "_", $v);
            $body[] = implode("\r\n", array(
                "Content-Disposition: form-data; name=\"{$k}\"; filename=\"{$v}\"",
                "Content-Type: application/octet-stream",
                "",
                $data,
            ));
        }

        // generate safe boundary
        do {
            $boundary = "---------------------" . md5(mt_rand() . microtime());
        } while (preg_grep("/{$boundary}/", $body));

        // add boundary for each parameters
        array_walk($body, function (&$part) use ($boundary) {
            $part = "--{$boundary}\r\n{$part}";
        });

        // add final boundary
        $body[] = "--{$boundary}--";
        $body[] = "";

        // set options
        return @curl_setopt_array($ch, array(
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => implode("\r\n", $body),
            CURLOPT_HTTPHEADER => array(
                "Expect: 100-continue",
                "Content-Type: multipart/form-data; boundary={$boundary}", // change Content-Type
            ),
        ));
    }
}